<template>
  <div>
    <MyTable :obj="goodsList">
      <template v-slot:header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #mytd="{ row: obj, inde: index }">
        <td>{{ obj.id }}</td>
        <td>{{ obj.goods_name }}</td>
        <td>{{ obj.goods_price }}</td>
        <td>
          <input
            v-fofo
            @keyup.enter="addFn(obj, obj.inputValue)"
            @keyup.esc="obj.inputValue = ''"
            @blur="obj.inputVisible = false"
            class="tag-input form-control"
            v-model="obj.inputValue"
            v-if="obj.inputVisible"
            type="text"
          />
          <button
            v-else
            @click="obj.inputVisible = true"
            class="btn btn-primary btn-sm add-tag"
          >
            +Tag
          </button>
          <span
            v-for="tag in obj.tags"
            :key="tag"
            class="badge badge-warning tags"
            >{{ tag }}</span
          >
        </td>
        <td>
          <button @click="delFn(index)" class="btn btn-danger btn-sm">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";
export default {
  components: {
    MyTable,
  },
  data() {
    return {
      goodsList: [],
    };
  },
  directives: {
    fofo: {
      inserted(el) {
        el.focus();
      },
    },
  },
  created() {
    this.$axios({
      url: "/api/goods",
    }).then((res) => {
      this.goodsList = res.data.data;
      console.log(this.goodsList);
    });
  },
  methods: {
    delFn(index) {
      this.goodsList.splice(index, 1);
    },
    addFn(obj, a) {
      if (a.trim().length == 0) {
        alert("gtmnll");
      } else {
        obj.tags.push(a);
        obj.inputValue = "";
      }
    },
  },
};
</script>

<style scoped>
.tags {
  margin-right: 3px;
}
</style>